package com.mck.controller;


import com.mck.entity.LoginInfo;
import com.mck.entity.Result;
import com.mck.entity.User;
import com.mck.service.UserService;
import com.mck.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/public/login")
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     */
    @PostMapping
    public Result login(@RequestBody User user) {
        LoginInfo info = userService.login(user);
        if (info == null) {
            return Result.error("用户名或密码错误");
        }

        // 生成token
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", info.getUserId());
        claims.put("userTypeId", info.getUserTypeId());
        claims.put("uname", info.getUname());
        String token = JwtUtil.generateToken(claims);

        Map<String, Object> data = new HashMap<>();
        data.put("token", token);
        data.put("userInfo", info);

        return Result.success(data);
    }
}
